
package com.dewod.producesales.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.dewod.producesales.model.response.PdaSnStatusVo;
import com.dewod.producesales.model.view.CabinetSn;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SubmitViewModel extends ViewModel {

    /**
     * 订单号
     */
    public MutableLiveData<String> orderNo = new MutableLiveData<>();

    /**
     * 是否验证通过
     */
    public MutableLiveData<Boolean> validated = new MutableLiveData<>(false);


    /**
     * 柜机SN 码列表（内部存储）
     */
    public MutableLiveData<List<CabinetSn>> cabinetSnList = new MutableLiveData<>(new ArrayList<>());

    /**
     * 一体机SN 码列表（内部存储）
     */
    public MutableLiveData<List<PdaSnStatusVo>> macSnList = new MutableLiveData<>(new ArrayList<>());

    /**
     * 锁控板SN 码列表（内部存储）
     */
    public MutableLiveData<List<PdaSnStatusVo>> lockSnList = new MutableLiveData<>(new ArrayList<>());

    /**
     * 是否所有SN码都选中
     */
    public MutableLiveData<Boolean> allChecked = new MutableLiveData<>(false);

    /**
     * 提示信息
     */
    public final MutableLiveData<String> toastMessage = new MutableLiveData<>();


    public void showSnDuplicateToast() {
        toastMessage.setValue("该条码已扫描");
    }

    public void showToast(String msg){
        toastMessage.postValue(msg);
    }

    public void updateOrderNo(String sn) {
        if(!TextUtils.equals(sn, orderNo.getValue())){
            orderNo.postValue(sn);
        }else{
            showSnDuplicateToast();
        }
    }

    /**
     * 扫描柜机SN码
     * @param cabinetSn
     */
    public void setCabinetSnChecked(String cabinetSn) {
        // 根据扫描的码，判断是否存在
        List<CabinetSn> sns = cabinetSnList.getValue();
        assert sns != null;
        CabinetSn sn = sns.stream()
                .filter(Objects::nonNull)
                .filter(csn -> csn.getSn().equals(cabinetSn))
                .findFirst()
                .orElse(null);

        if(sn != null ){
            sn.setChecked(Boolean.TRUE);
            cabinetSnList.postValue(sns);
        }else{
            this.showToast("该条码与订单无关");
        }
    }

    public void checkIsAllChecked(){
        List<CabinetSn> sns = cabinetSnList.getValue();
        assert sns != null;
        if(sns.isEmpty()){
            return;
        }
        if(sns.stream().filter(Objects::nonNull).allMatch(CabinetSn::getChecked)){
            allChecked.postValue(Boolean.TRUE);
            toastMessage.postValue("所有柜机码已扫描");
        }
    }



    public void updateIntegratedMacSn(String sn) {
        List<PdaSnStatusVo> currentList = macSnList.getValue();
        if (currentList == null) {
            currentList = new ArrayList<>();
        }

        boolean exists = currentList.stream().anyMatch(item -> TextUtils.equals(item.getSn(), sn));
        if (!exists) {
            currentList.add(new PdaSnStatusVo(sn, 2));
            macSnList.postValue(currentList);
            this.toastMessage.setValue(null);
        } else {
            showSnDuplicateToast();
        }
    }

    public void updateLockCtlBoardSn(String sn) {
        List<PdaSnStatusVo> currentList = lockSnList.getValue();
        if (currentList == null) {
            currentList = new ArrayList<>();
        }

        boolean exists = currentList.stream().anyMatch(item -> TextUtils.equals(item.getSn(), sn));
        if (!exists) {
            currentList.add(new PdaSnStatusVo(sn, 3));
            lockSnList.postValue(currentList);
            this.toastMessage.setValue(null);
        } else {
            showSnDuplicateToast();
        }
    }

    public void removeMacSn(String sn) {
        List<PdaSnStatusVo> currentList = macSnList.getValue();
        if (currentList != null) {
            currentList.removeIf(item -> TextUtils.equals(item.getSn(), sn));
            macSnList.postValue(new ArrayList<>(currentList));  // 使用新对象避免引用污染
        }
    }

    public void removeLockSn(String sn) {
        List<PdaSnStatusVo> currentList = lockSnList.getValue();
        if (currentList != null) {
            currentList.removeIf(item -> TextUtils.equals(item.getSn(), sn));
            lockSnList.postValue(new ArrayList<>(currentList));
        }
    }

    public PdaSnStatusVo getSn(List<PdaSnStatusVo> list, String sn) {
        if (list == null) {
            return null;
        }
        return list.stream().filter(Objects::nonNull).filter(item -> TextUtils.equals(item.getSn(), sn)).findFirst().orElse(null);
    }

    public void clearAll() {
        orderNo.postValue("");
        cabinetSnList.postValue(new ArrayList<>());
        macSnList.postValue(new ArrayList<>());
        lockSnList.postValue(new ArrayList<>());

        validated.postValue(false);
        allChecked.postValue(false);
    }

}